﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using GoDive.Contract;
using GoDive.Service;
using System.Linq.Expressions;
using GoDive.Common;

namespace GoDive.App
{

    public class DestinationController : ApiController
    {

        GoDiveDBContext db = new GoDiveDBContext();

        [HttpGet]
        public PagedListContract<tdescomment> GetCommentPagedList([FromUri]int destID, [FromUri] int pageindex, [FromUri]int pagesize)
        {
            var query =
              (from comment in db.tdescomment
               join user in db.tuser
               on comment.UserID equals user.ID
               where comment.DestinationID == destID
               select new
           {
               ID = comment.ID,
               UserID = comment.UserID,
               UserName = user.UserName,
               UserLevel = user.Level,
               UserAvatar = user.AvatarMiddle,
               PariseCount = comment.PariseCount,
               DataChangeTime = comment.DataChangeTime,
               Content = comment.Content,
               Score = comment.Score,
               Pic1 = comment.Pic1,
               Pic2 = comment.Pic2,
               Pic3 = comment.Pic3,
               Pic4 = comment.Pic4,
               Pic5 = comment.Pic5
           }).OrderByDescending(x => x.PariseCount).ThenByDescending(x => x.DataChangeTime);

            PagedListContract<dynamic> pagedList = new PagedListContract<dynamic>
            {
                PageIndex = pageindex,
                PageSize = pagesize
            };

            pagedList.Rows = query.GetPagedList(pagedList).AsEnumerable();

            PagedListContract<tdescomment> comments = new PagedListContract<tdescomment>
            {
                PageIndex = pageindex,
                PageSize = pagesize,
                PageTotalCount = pagedList.PageTotalCount,
                Total = pagedList.Total,
                Rows = pagedList.Rows.Select(x => new tdescomment
                {
                    ID = x.ID,
                    User = new tuser
                    {
                        ID = x.UserID,
                        UserName = x.UserName,
                        Level = x.UserLevel,
                        AvatarMiddle = x.UserAvatar
                    },
                    PariseCount = x.PariseCount,
                    DataChangeTime = x.DataChangeTime,
                    Content = x.Content,
                    Score = x.Score,
                    Pic1 = x.Pic1,
                    Pic2 = x.Pic2,
                    Pic3 = x.Pic3,
                    Pic4 = x.Pic4,
                    Pic5 = x.Pic5
                })
            };
            return comments;
        }


        public PagedListContract<tdestinationalbum> GetDestinationAlbum(int desid, int pageindex, int pagesize)
        {
            PagedListContract<tdestinationalbum> pagedParams = new PagedListContract<tdestinationalbum>
            {
                PageIndex = pageindex,
                PageSize = pagesize
            };

            var destinationPics = db.tdestinationalbum.Where(x => x.DestinationID == desid).OrderByDescending(x => x.ID).GetPagedList(pagedParams);

            pagedParams.Rows = destinationPics.ToList();

            return pagedParams;
        }

    }
}
